package com.carlinfo.userscenter.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.EncryptUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAUserDao;
import com.carlinfo.userscenter.users.pojo.AUsers;

public class AUsersDaoImpl extends BaseDaoImpl<AUsers> implements IAUserDao
{
	private EncryptUtil encryptUtil = new EncryptUtil();
	public AUsersDaoImpl()
	{
		AUsers user = new AUsers();
		this.baseObj = user;
	}

	@Override
	public int saveOneDao(AUsers user)
	{
		String sql = "insert into a_users values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> list = new ArrayList<Object>();
		list.add(user.getRegionId());
		list.add(user.getEmail());
		String password = encryptUtil.encodeStr(user.getPassword());
		list.add(password);
		list.add(user.getNickName());
		list.add(user.getTrueName());
		list.add(user.getQq());
		list.add(user.getPhone());
		list.add(user.getBirthday());
		list.add(user.getLoginCount());
		list.add(user.getFailedCount());
		list.add(user.getFailedTime());
		list.add(user.getAddress());
		list.add(user.getPhotoPath());
		list.add(user.getEmailStatus());
		list.add(user.getPhoneStatus());
		list.add(user.getIdcardStatus());
		list.add(user.getSex());
		list.add(user.getStatus());
		list.add(user.getCreateTime());
		list.add(user.getUpdateTime());
		list.add(user.getLastLoginTime());
		Map<String, Object> condMap = new HashMap<String, Object>();

		int res = this.excJDBC(sql, list, condMap);
		return res;
	}

	@Override
	public List<AUsers> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from a_users where 1=1 ";
		/*
		 * 关键字: select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%'); 状态 : select * from a_dynasty where status = 1 ; 时间 :
		 * select * from a_dynasty where (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 关键字+状态: select * from a_dynasty where (name like
		 * '%0%' or capital like '%0%' or context like '%0%') and status = 1 ; 关键字+时间:
		 * select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%') and (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 时间+状态: select * from a_dynasty where status = 1 and
		 * (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
		 * 关键字+状态+时间: select * from a_dynasty where (name like '%0%' or capital like
		 * '%0%' or context like '%0%') and status = 1 and (pubTime >= '2018-09-18
		 * 00:00:00' and pubTime < '2018-09-19 10:00:00'); 啥也不选: select * from
		 * a_dysnasty ;
		 */

		/* 根据条件来组装sql语句 */
		if (condMap.get("keyWord") != null && !"".equalsIgnoreCase(condMap.get("keyWord") + ""))
		{
			sql += " and (qq like ? or nickName like ? or trueName	 like ?) ";
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			list.add(condMap.get("status"));
		}
		if (condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			sql += " and  (updateTime >= ? and updateTime < ?) ";
			list.add(condMap.get("stDate"));
			list.add(condMap.get("edDate"));
		}

		/**
		 * 设置排序方式
		 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc ";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc,nickName desc ";
		} else
		{
			sql += " order by updateTime desc ";
		}

		if (pageInfoUtil != null)
		{
			return this.findListByPage(pageInfoUtil, sql, list);
		}
		return this.findList(sql, list);
	}

	@Override
	public AUsers selectOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from a_users ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ? limit 1;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("nickName") + "")) && condMap.get("nickName") != null)
		{
			sql += "where nickName = ? limit 1;";
			list.add(condMap.get("nickName"));
		}else if (!"".equalsIgnoreCase((condMap.get("email") + "")) && condMap.get("email") != null)
		{
			sql += "where email = ? limit 1;";
			list.add(condMap.get("email"));
		}
		return this.findONeJDBC(sql, list);
	}

	@Override
	public int updateOneDao(AUsers user)
	{
		String sql = "update a_users set regionId=?,email=?,password=?,nickName=?"
				+ ",trueName=?,qq=? ,phone=?,birthday=?,loginCount=? "
				+ ",failedCount=?,failedTime=? ,address=?,photoPath=?,emailStatus=?"
				+ ",phoneStatus=?,idcardStatus=? ,sex=?,status=?,createTime=?"
				+ ",updateTime=?,lastLoginTime=? where id = ?";
		List<Object> list = new ArrayList<Object>();
		list.add(user.getRegionId());
		list.add(user.getEmail());
		list.add(user.getPassword());
		list.add(user.getPassword());
		list.add(user.getNickName());
		list.add(user.getTrueName());
		list.add(user.getQq());
		list.add(user.getPhone());
		list.add(user.getBirthday());
		list.add(user.getLoginCount());
		list.add(user.getFailedCount());
		list.add(user.getFailedTime());
		list.add(user.getAddress());
		list.add(user.getPhotoPath());
		list.add(user.getEmailStatus());
		list.add(user.getPhoneStatus());
		list.add(user.getIdcardStatus());
		list.add(user.getSex());
		list.add(user.getStatus());
		list.add(user.getCreateTime());
		list.add(user.getUpdateTime());
		list.add(user.getLastLoginTime());
		list.add(user.getId());
		return this.excJDBC(sql, list);
	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "delete from a_users ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ?;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("nickName") + "")) && condMap.get("nickName") != null)
		{
			sql += "where nickName = ? ;";
			list.add(condMap.get("nickName"));
		}
		return this.excJDBC(sql, list);
	}

	@Override
	public int selectMaxId()
	{
		return 0;
	}

}
